declare let eManager;
const {ccclass, property} = cc._decorator;
import Utils = require("../utils/Utils");
const baozhaPos = [
    cc.v2(-53,-25),
    cc.v2(-85,-267),
    cc.v2(130,-340)
];
//1-1 祛痘
@ccclass
export default class QuDou extends cc.Component {
    @property
    stepName = "";
    //粉刺针 
    @property(cc.Node)
    zhen: cc.Node = null;
    //粉刺圈
    @property(cc.Node)
    quan: cc.Node = null;
    //粉针刺碰撞盒
    @property(cc.Node)
    fenciZhenPeng: cc.Node = null;

    //粉针圈碰撞盒
    @property(cc.Node)
    fenciQuanPeng: cc.Node = null;

    //痘痘
    @property(cc.Node)
    doudou: cc.Node[] = [];
    //红圈碰撞区
    @property(cc.Node)
    hongQuan: cc.Node[] = [];

    //痘痘节点  需要动态改层级
    @property(cc.Node)
    douNode: cc.Node = null;

    //音效
    // @property({type:cc.AudioClip})
    // chuoDou: cc.AudioClip = null;

    // @property({type:cc.AudioClip})
    // jiDou: cc.AudioClip = null;

    _zha_list = [];//扎破的痘痘
    _done_list= [];//挤完的痘痘

    _isPlay = false;
    _startPos = cc.v3(230,-420);
    _upIndex = 0;
    _downIndex = 0;
    start(){
        this.zhen.active = true;
        this.quan.active = false;
        this._upIndex = this.quan.getSiblingIndex();
        this._downIndex = this.douNode.getSiblingIndex();
    };

    initData(){
        this._zha_list = [];
        this._done_list = [];
        this.zhen.position = this._startPos;
        this.quan.position = this._startPos;
        eManager.emit("REFRESH_STEP_NAME", this.stepName);
        eManager.emit("HZ_REFRESH_ICON_TIPS", "step_1_1_1");
    };

    _touchStart(event){
        if(this._isPlay)
            return;
        if(this._zha_list.length >= 3 && this._done_list.length >= 3)
            return;
        let worldPoint = event.touch.getLocation();
        if(this._zha_list.length >= 3){
            let localPoint = this.quan.parent.convertToNodeSpaceAR(worldPoint);
            this.quan.x = localPoint.x;
            this.quan.y = localPoint.y;
        }else{
            let localPoint = this.zhen.parent.convertToNodeSpaceAR(worldPoint);
            this.zhen.x = localPoint.x;
            this.zhen.y = localPoint.y;
        }
    };
    
    _touchMove(event:any){
        if(this._isPlay)
            return;
        if(this._zha_list.length >= 3 && this._done_list.length >= 3)
            return;
        // let delta = event.touch.getDelta();
        // if(this._zha_list.length >= 3){
        //     this.quan.x += delta.x;
        //     this.quan.y += delta.y;
        // }else{
        //     this.zhen.x += delta.x;
        //     this.zhen.y += delta.y;
        // }
        let worldPoint = event.touch.getLocation();
        if(this._zha_list.length >= 3){
            let localPoint = this.quan.parent.convertToNodeSpaceAR(worldPoint);
            this.quan.x = localPoint.x;
            this.quan.y = localPoint.y;
        }else{
            let localPoint = this.zhen.parent.convertToNodeSpaceAR(worldPoint);
            this.zhen.x = localPoint.x;
            this.zhen.y = localPoint.y;
        }
        let peng = this._zha_list.length >= 3 ? this.fenciQuanPeng.getBoundingBoxToWorld()
            : this.fenciZhenPeng.getBoundingBoxToWorld();
        for (let index = 0; index < this.hongQuan.length; index++) {
            let quan = this.hongQuan[index];
            let box = quan.getBoundingBoxToWorld();
            if(peng.intersects(box)){
                this.playDouAni(index);
                break;
            }
        }
    };

    //破痘动画
    playDouAni(index:number){
        if(this._zha_list.length >= 3 && this._done_list.length < 3){//挤痘
            if(this._done_list.indexOf(index) == -1){
                this._done_list.push(index);
                this._isPlay = true;
                this.quan.setSiblingIndex(this._downIndex);
                this.douNode.setSiblingIndex(this._upIndex);
                let moveto = cc.moveTo(0.2, baozhaPos[index]);
                this.quan.runAction(moveto);
                let spine = this.doudou[index].getComponent(sp.Skeleton);
                this.scheduleOnce(()=>{
                    Utils.audioManager.playSound("huazhuang/jidou",0.8);
                    eManager.emit("SET_VIBRATE", 800);
                },0.5);
                spine.setCompleteListener(()=>{
                    this._isPlay = false;
                    this.quan.setSiblingIndex(this._upIndex);
                    this.douNode.setSiblingIndex(this._downIndex);
                    this.hongQuan[index].active = false;
                    if(this._done_list.length >= 3){
                        this.quan.active = false;
                        eManager.emit("DONE_POINT", 1);
                        eManager.emit("MINI_DONE_POINT", 1);
                    }
                });
                spine.setAnimation(0,"3",false);
            }
        }else{
            if(this._zha_list.length < 3){
                if(this._zha_list.indexOf(index) == -1){//扎痘
                    this._zha_list.push(index);
                    this._isPlay = true;
                    let spine = this.doudou[index].getComponent(sp.Skeleton);
                    this.scheduleOnce(()=>{
                        Utils.audioManager.playSound("huazhuang/chuodou",0.8);
                        eManager.emit("SET_VIBRATE", 600);
                    },0.5);
                    spine.setCompleteListener(()=>{
                        this._isPlay = false;
                        if(this._zha_list.length >= 3){
                            eManager.emit("HZ_REFRESH_ICON_TIPS", "step_1_1_2");
                            eManager.emit("HZ_MAKE_DOT", {
                                makeup_close:1,
                                makeup_step:this.stepName
                            });
                            this.zhen.active = false;
                            this.quan.active = true;
                            this.quan.position = this.zhen.position;
                            this.zhen.x = 10000;
                        }
                    });
                    let zhenAni = this.zhen.getComponent(cc.Animation);
                    if(zhenAni){
                        zhenAni.play("step1_zhentou" + (index +1));
                        this.scheduleOnce(()=>{
                            spine.setAnimation(0,"2",false);
                        },0.33);
                    }
                }
            }
        }
    };

    setDouShow(isShow:boolean){
        for (let index = 0; index < this.doudou.length; index++) {
            this.doudou[index].active = isShow == true;
        }
    };

}
